﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UtilsCache;
using UtilsORM;
using UtilsORM.MySqlData;
using static System.Formats.Asn1.AsnWriter;

namespace ServiceWeb.DBGateway
{
    public class DBIotProduct
    {

        EFYihongIotContext ef = new EFYihongIotContext();

        private static object obj = new object();
        public bool saveAll(MagerLoginInfo LoginUserInfo, iot_product reqModel)
        {
            lock (obj)
            {

                //先查一下有没重复提交
                iot_product model = ef.iot_product.Where(c => c.p_guid == reqModel.p_guid).FirstOrDefault();
                if (model == null)
                {
                    //查编码
                    iot_product getcode = ef.iot_product.OrderByDescending(c => c.p_id).FirstOrDefault();
                    if (getcode != null)
                    {
                        string p_code = getcode.p_code.Replace("p", "");
                        reqModel.p_code = "p" + (int.Parse(p_code) + 1).ToString();
                    }
                    else
                    {
                        reqModel.p_code = "p1001";
                    }
                    //写入
                    reqModel.p_add_time = DateTime.Now;
                    reqModel.p_c_guid = LoginUserInfo.userCompanyGuid;
                    reqModel.p_check_code = reqModel.p_code + new UtilsBase.UtilsStringQuery().GetRandNumber2(); ;
                    ef.iot_product.Add(reqModel);
                    ef.SaveChanges();

                }
                else
                {
                    model.p_scene_name = reqModel.p_scene_name;
                    model.p_status = reqModel.p_status;
                    model.p_name = reqModel.p_name;
                    model.p_status = reqModel.p_status;
                    model.p_mod_time = DateTime.Now;
                    ef.iot_product.Update(model);
                    ef.SaveChanges();

                    updateProductClientGuid(model.p_guid, model.p_c_guid);
                }
            }
            return true;

        }
        /// <summary>
        /// 更新产品客户信息
        /// </summary>
        /// <param name="p_guid"></param>
        /// <param name="c_guid"></param>
        private void updateProductClientGuid(string p_guid, string c_guid)
        {
            ef.iot_device.Where(c => c.d_p_guid == p_guid).ExecuteUpdate(c => c.SetProperty(p => p.d_c_guid, c_guid));
            ef.iot_device_task_time_interval.Where(c => c.ti_p_guid == p_guid).ExecuteUpdate(c => c.SetProperty(p => p.ti_c_guid, c_guid));
            ef.iot_device_task_time_fixed.Where(c => c.tf_p_guid == p_guid).ExecuteUpdate(c => c.SetProperty(p => p.tf_c_guid, c_guid));
            ef.iot_device_task_default_run.Where(c => c.dr_p_guid == p_guid).ExecuteUpdate(c => c.SetProperty(p => p.dr_c_guid, c_guid));
            ef.iot_device_task_plain.Where(c => c.tp_p_guid == p_guid).ExecuteUpdate(c => c.SetProperty(p => p.tp_c_guid, c_guid));
            ef.iot_device_linkage.Where(c => c.l_c_guid == p_guid).ExecuteUpdate(c => c.SetProperty(p => p.l_c_guid, c_guid));

        }
        public bool changeStatus(string dguid)
        {
            iot_product product = ef.iot_product.Where(c => c.p_guid == dguid).FirstOrDefault();
            if (product != null)
            {
                if (product.p_status == 0)
                {
                    product.p_status = 1;
                }
                else
                {
                    product.p_status = 0;
                }
                ef.SaveChanges();
            }
            return true;
        }
        public iot_product getProduct(string pguid)
        {
            return ef.iot_product.Where(c => c.p_guid == pguid).FirstOrDefault();
        }
        /// <summary>
        /// 校验码
        /// </summary>
        /// <param name="pCheckCode"></param>
        /// <returns></returns>
        public iot_product getProductBypCheckCode(string pCheckCode)
        {
            return ef.iot_product.Where(c => c.p_check_code == pCheckCode).FirstOrDefault();

        }
        public List<iot_product> getProductList(string cguid)
        {
            return ef.iot_product.Where(c => c.p_c_guid == cguid).ToList();
        }


        public void setPruductStateValue(string pguid)
        {
            EFYihongIotContext ef = new EFYihongIotContext();
            iot_product_state state = ef.iot_product_state.Where(c => c.ps_p_guid == pguid).FirstOrDefault();
            if (state != null)
            {
                //1停止，0启动
                if (state.ps_status == 0)
                {
                    state.ps_status = 1;
                    //更新为停止，更新状态

                    ef.iot_device_task_plain.Where(c => c.tp_p_guid == pguid && c.tp_auto_run_join == 1).ExecuteUpdate(c => c.SetProperty(p => p.tp_auto_run, 1));
                    ef.iot_device_linkage.Where(c => c.l_p_guid == pguid && c.l_auto_run_join == 1).ExecuteUpdate(c => c.SetProperty(p => p.l_auto_run, 1));

                    ////删除未完成的流程和条件
                    List<iot_device_linkage> linkages = ef.iot_device_linkage.Where(c => c.l_p_guid == pguid && c.l_auto_run_join == 1).ToList();
                    foreach (iot_device_linkage item in linkages)
                    {
                        ef.iot_device_linkage_condition_handle.Where(c => c.lc_l_guid == item.l_guid).ExecuteDelete();
                    }

                    //删除未完成的流程和条件
                    List<iot_device_task_plain> plains = ef.iot_device_task_plain.Where(c => c.tp_p_guid == pguid && c.tp_auto_run_join == 1).ToList();
                    foreach (iot_device_task_plain item in plains)
                    {
                        ef.iot_device_task_plain_item_handle.Where(c => c.tpi_tp_guid == item.tp_guid && c.tpi_type==100).ExecuteUpdate(c => c.SetProperty(p => p.tpi_value, "0")
                         .SetProperty(p => p.tpi_execute_delayed_time, DateTime.Now));

                        ef.iot_device_task_plain_item.Where(c => c.tpi_tp_guid == item.tp_guid).ExecuteUpdate(c => c.SetProperty(p => p.tpi_complete,1)
                       .SetProperty(p => p.tpi_complete_time, DateTime.Now));

                        ef.iot_device_task_plain.Where(c=>c.tp_guid==item.tp_guid).ExecuteUpdate(c => c.SetProperty(p => p.tp_complete,1)
                         .SetProperty(p => p.tp_complete_time, DateTime.Now));
                    }
                }
                else
                {
                    //启动
                    state.ps_status = 0;
                    ef.iot_device_task_plain.Where(c => c.tp_p_guid == pguid && c.tp_auto_run_join == 1).ExecuteUpdate(c => c.SetProperty(p => p.tp_auto_run, 0));
                    ef.iot_device_linkage.Where(c => c.l_p_guid == pguid && c.l_auto_run_join == 1).ExecuteUpdate(c => c.SetProperty(p => p.l_auto_run, 0));
                }

                ef.iot_product_state.Where(c => c.ps_p_guid == pguid).ExecuteUpdate(c =>
                c.SetProperty(p => p.ps_status, state.ps_status));

                //删除条件处理的缓存,按最新的条件执行
                new DBIotDeviceLinkage().delLinkageRedisCache();

                //////如果是停止自动运行，删除未完成的流程和触发
                //List<iot_device_task_plain> listplians = ef.iot_device_task_plain.Where(c => c.tp_p_guid == pguid).ToList();
                //foreach (iot_device_task_plain item in listplians)
                //{
                //    ef.iot_device_task_plain_item_handle.Where(c => c.tpi_tp_guid == item.tp_guid && c.tpi_type == 100).ExecuteUpdate(c => c.SetProperty(p => p.tpi_value, "0")
                //    .SetProperty(p=>p.tpi_execute_delayed_time,DateTime.Now));

                //  //  ef.iot_device_task_plain_item_handle.Where(c => c.tpi_tp_guid == item.tp_guid).ExecuteDelete();
                //}

                //List<iot_device_linkage> listlinkages = ef.iot_device_linkage.Where(c => c.l_p_guid == pguid).ToList();
                //foreach (iot_device_linkage item in listlinkages)
                //{
                //    ef.iot_device_linkage_condition_handle.Where(c => c.lc_l_guid == item.l_guid).ExecuteDelete();
                //}

            }
            else
            {
                state = new iot_product_state();
                state.ps_p_guid = pguid;
                state.ps_guid = Guid.NewGuid().ToString();
                state.ps_add_time = DateTime.Now;
                state.ps_status = 0;
                //state.ps_cu_guid = clientLoginUser.userGuid;
                ef.iot_product_state.Add(state);
                ef.SaveChanges();
            }
        }

        public iot_product_state getPruductStateValue(string pguid)
        {
            EFYihongIotContext ef = new EFYihongIotContext();
            return ef.iot_product_state.Where(c => c.ps_p_guid == pguid).FirstOrDefault();

        }
    }
}
